<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/9/8
 * Time: 15:51
 */
if(!session_id()) session_start();
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Methods:POST,GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

require_once('dbServerConnect.php');
$con = new DB();
$link = $con->mySqlServer();

if (!$link) {
    echo '数据库连接失败';
    return;
}


$query = ""; $pageNum = 0; $pageSize = 1;
$query = $_GET['query'];
$pageNum = $_GET['pagenum'];
$pageSize = $_GET['pagesize'];

$arrayList = array(array('id'=>'','username'=>'','comment'=>'','topic_title'=>'','createdate'=>'','total'=>''));


if($query == ""){
    if($pageNum != 0) {
        $pageNum = ($pageNum - 1) * $pageSize;
    }
    $sql = "select * from comment limit $pageNum, $pageSize";
    $result = mysqli_query($link,$sql) or die(mysqli_error($link));
    // 获取总行数
    $total = mysqli_num_rows(mysqli_query($link,"select * from comment"));
    $res = mysqli_fetch_all($result);
    if ($res) {
        echo getBlogContainer($res,$total);
    }
    else {
        echo false;
    }
}
else {
    if($pageNum != 0) {
        $pageNum = ($pageNum - 1) * $pageSize;
    }
    $sql = "select * from comment where comment like '%$query%'limit $pageNum, $pageSize";
    $result = mysqli_query($link,$sql) or die(mysqli_error($link));
    // 获取总行数
    $total = mysqli_num_rows(mysqli_query($link,"select * from comment"));
    $res = mysqli_fetch_all($result);
    if ($res) {
        echo getBlogContainer($res,$total);
    }
    else {
        echo false;
    }
}



function getBlogContainer($res,$total) {
    for ($i = 0;$i < sizeof($res); $i++) {
        $arrayList[$i]['id'] = $res[$i][0];
        $arrayList[$i]['blog_container_id'] = $res[$i][1];
        $arrayList[$i]['username'] = $res[$i][2];
        $arrayList[$i]['comment'] = $res[$i][3];
        $arrayList[$i]['createdate'] = $res[$i][4];
        $arrayList[$i]['topic_title'] = $res[$i][9];
    }
    $arrayList[0]['total'] = $total;
    return json_encode($arrayList);
}